package com.huatai.datacenter.controller;

import com.alibaba.fastjson.JSONObject;
import com.huatai.common.api.R;
import com.huatai.common.httputil.HttpRequest;
import com.huatai.datacenter.entity.ApiEntity;
import com.huatai.datacenter.service.ApiService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.annotation.Annotation;
import java.util.*;

/**
 * @author Lion
 * @date 2023/6/19  10:51
 */
@RequestMapping("/apiInfo")
@RestController
@Api(value = "api服务",tags = "api服务")
public class ApiController extends BaseController<ApiService, ApiEntity>{

	@Autowired
	WebApplicationContext applicationContext;

	@ApiOperation("新增api服务")
	@Transactional
	@Override
	public R save(@ApiParam(required = true) @RequestBody ApiEntity data) {

		this.service.SaveOrUpdateWithValid(data);
		this.service.saveAPIApply(data);
		return R.success("保存成功");
	}

	@ApiOperation("发送请求")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "url", value = "主题任务id", dataType = "String"),
		@ApiImplicitParam(name = "body", value = "主题任务id", dataType = "JSONObject")
	})
	@PostMapping("/send")
	public R sendRequest(@RequestBody JSONObject data){

		String method = data.getString("method");
		String url = data.getString("url");
		String body = data.getJSONObject("body").toString();
		String result = this.service.sendData(method,url,body);

		return R.success(result);
	}

}

